package pos.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import empleados.DiaLibre;

public class JDBCDiaLibreDAO implements IDiaLibreDAO{
	
private Connection conn;
	
	public JDBCDiaLibreDAO(){		
		conn = ConnectionManager.getInstance().checkOut();	
	}

	@Override
	public void delete(String dniemp, Date fechaini) {
		String sql = "DELETE FROM diaslibres WHERE (dniemp = ? AND fechaini = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dniemp);
            stmt.setDate(2, fechaini);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
		
	}

	@Override
	public void insert(DiaLibre d) {
		String sql = "INSERT INTO diaslibres (dniemp, fechaini, fechafin, estado) VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, d.getDNIemp());
            stmt.setDate(2, d.getFechaIni());
            stmt.setDate(3, d.getFechaFin());
            stmt.setString(4, d.getEstado());           
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}

	@Override
	public void modificarestado(DiaLibre d, String estado) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		String sql = "UPDATE diaslibres SET estado = ?"+
				"WHERE (dniemp = ? AND fechaini=?)";
		
		try{
			stmt = conn.prepareStatement(sql);			
			stmt.setString(1, estado);
			stmt.setString(2, d.getDNIemp());
			stmt.setDate(3, d.getFechaIni());		
			stmt.executeUpdate();
		}catch(SQLException ex){
			System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
		
	}

	@Override
	public DiaLibre select(String dniemp, Date fechaini) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        DiaLibre dialibre = null;
       
        String sql = "SELECT * FROM diaslibres WHERE (dniemp = ? AND fechaini=?)";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dniemp);
            stmt.setDate(2, fechaini);
            result = stmt.executeQuery();

            if(result.next()){
            	dialibre = new DiaLibre();
            	dialibre.setDNIemp(result.getString("dniemp"));
            	dialibre.setFechaIni(result.getDate("fechaini"));
            	dialibre.setFechaFin(result.getDate("fechafin"));
            	dialibre.setEstado(result.getString("estado"));	
            	
            }
        } catch (SQLException u) {
            System.out.println("Message: " + u.getMessage());
            System.out.println("SQLState: " + u.getSQLState());
            System.out.println("ErrorCode: " + u.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException u) {
            }
        }
        return dialibre;
	}

	@Override
	public List<DiaLibre> selectdialibreporemp(String dniemp) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<DiaLibre> listaDL = new LinkedList<DiaLibre>();
       
        String sql = "SELECT * FROM diaslibres WHERE (dniemp = ?) ORDER BY fechaini";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dniemp);
            result = stmt.executeQuery();
            while(result.next()){
            	DiaLibre d = new DiaLibre();
        		d.setDNIemp(result.getString("dniemp"));
            	d.setFechaIni(result.getDate("fechaini"));
            	d.setFechaFin(result.getDate("fechafin"));
            	d.setEstado(result.getString("estado"));
            	listaDL.add(d);
            	
            }
        } catch (SQLException u) {
            System.out.println("Message: " + u.getMessage());
            System.out.println("SQLState: " + u.getSQLState());
            System.out.println("ErrorCode: " + u.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException u) {
            }
        }
        return listaDL;
	}

	@Override
	public List<DiaLibre> selectdialibreporestado(String estado) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<DiaLibre> listaDL = new ArrayList<DiaLibre>();
       
        String sql = "SELECT * FROM diaslibres WHERE (estado = ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, estado);
            result = stmt.executeQuery();
 
            while(result.next()){
            	DiaLibre d = new DiaLibre();
        		d.setDNIemp(result.getString("dniemp"));
            	d.setFechaIni(result.getDate("fechaini"));
            	d.setFechaFin(result.getDate("fechafin"));
            	d.setEstado(result.getString("estado"));
            	listaDL.add(d);
            	
            }
        } catch (SQLException u) {
            System.out.println("Message: " + u.getMessage());
            System.out.println("SQLState: " + u.getSQLState());
            System.out.println("ErrorCode: " + u.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException u) {
            }
        }
        return listaDL;
	}
	
	public void finalize(){
		ConnectionManager.getInstance().checkIn(conn);
		
	}
	

}
